using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;

public partial class PoolEdt : BasePage
{
    SortedView poolsSorted;
    
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request["PoolName"] != null)
                Session["PoolName"] = Request["PoolName"];

            ResetSortControls();
            SearchAndBind(int.Parse(tbPage.Text), tbSortDirection.Text, "");

            if (Request["Add"] != null)
                btnAddPool_Click(sender, e);
        }
    }

    public void SearchAndBind(int _pageIndex, string sortDirection, string sortProperty, string commandName )
    {
        AdminServices.PoolSearchRequest searchRequest = new AdminServices.PoolSearchRequest();
        if (Session["PoolName"] != null)
            searchRequest.DisplayName = Session["PoolName"].ToString();
        searchRequest.InServiceProvider = true;


        AdminServices.AdminServices proxy = new AdminServices.AdminServices();
        
        AdminServices.Pool[] pools = proxy.PoolSearch(CurrentPrincipal, searchRequest);
        poolsSorted = new SortedView(pools);

        if (sortProperty != "")
        {
            ListSortDirection lsd;
            if (sortDirection == "ASC")
                lsd = ListSortDirection.Ascending;
            else
                lsd = ListSortDirection.Descending;
            poolsSorted.ApplySort(sortProperty, lsd);
        }

        Session.Add("PoolsSorted", poolsSorted);

        gvPool.DataSource = poolsSorted;
        gvPool.PageIndex = _pageIndex;

        if (commandName != "EditPool")
            gvPool.EditIndex = -1;

        gvPool.DataBind();
    }

    protected void gvPool_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (tbSortDirection.Text == "ASC" && tbSortProperty.Text == e.SortExpression)
            tbSortDirection.Text = "DESC";
        else
            tbSortDirection.Text = "ASC";

        tbSortProperty.Text = e.SortExpression;
        SearchAndBind(int.Parse(tbPage.Text), tbSortDirection.Text, e.SortExpression);
    }

    private void ResetSortControls()
    {
        tbPage.Text = "0";
        tbSortDirection.Text = "ASC";
        tbSortProperty.Text = "";
    }

    private void SearchAndBind(int _pageIndex, string sortDirection, string sortProperty)
    {
        SearchAndBind(_pageIndex, sortDirection, sortProperty, "");
    }

    protected void btnAddPool_Click(object sender, EventArgs e)
    {
        poolsSorted = (SortedView)Session["PoolsSorted"];        
        AdminServices.Pool[] poolsNew = new AdminServices.Pool[poolsSorted.Count +1];
        poolsSorted.CopyTo(poolsNew,1);
      
        AdminServices.Pool pool = new AdminServices.Pool();
        pool.PoolID = -1;
        pool.PoolName = "";
        poolsNew[0] = pool;

        poolsSorted = new SortedView(poolsNew);
        
        //Session.Add("PoolsSorted", poolsSorted);

        gvPool.EditIndex = 0;
        gvPool.PageIndex = 0;
        gvPool.DataSource = poolsSorted;
        gvPool.DataBind();
    }
    protected void btnSavePool_Click(object sender, EventArgs e)
    {
        int poolIndex = gvPool.EditIndex;
        if (poolIndex > -1)
        {
            gvPool.UpdateRow(poolIndex, false);
        }
  
    }

    protected void gvPool_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        AdminServices.AdminServices proxy = new AdminServices.AdminServices();
        poolsSorted = (SortedView)Session["PoolsSorted"];

        //unlock all
        proxy.PoolUnlockAll(CurrentPrincipal);

        switch (e.CommandName)
        {
            case "EditPool":
                //lock record
                proxy.PoolLock(CurrentPrincipal, Convert.ToInt32(gvPool.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text)); //((AdminServices.Pool)poolsSorted[int.Parse(e.CommandArgument.ToString())]).PoolID);
                //turn grid to edit mode
                gvPool.EditIndex = int.Parse(e.CommandArgument.ToString());  
                break;
            case "DeletePool":
                //lock record
                proxy.PoolLock(CurrentPrincipal, Convert.ToInt32(gvPool.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text)); //((AdminServices.Pool)poolsSorted[int.Parse(e.CommandArgument.ToString())]).PoolID);
                //delete record
                proxy.PoolDelete(CurrentPrincipal, Convert.ToInt32(gvPool.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text)); //((AdminServices.Pool)poolsSorted[int.Parse(e.CommandArgument.ToString())]).PoolID);
                //unlock all
                proxy.PoolUnlockAll(CurrentPrincipal);
                break;
            case "CancelPool":
                //Cancel Edit
                gvPool.EditIndex = -1;
                break;
        }
        SearchAndBind(int.Parse(tbPage.Text), tbSortDirection.Text, tbSortProperty.Text, e.CommandName);
    }
    protected void gvPool_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.Cells[0].Text == "-1") 
        {
            e.Row.Cells[2].Controls[0].Visible = false; 
            e.Row.Cells[3].Controls[0].Visible = false;          
            e.Row.Cells[0].Text = "";
        }
    }

    protected void gvPool_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string fID = Utilities.getCellValue((GridView)sender, e, 0, 0);
        string fPoolName = Utilities.getCellValue((GridView)sender, e, 1, 1);

        AdminServices.AdminServices proxy = new AdminServices.AdminServices();

        poolsSorted = (SortedView)Session["PoolsSorted"];
        AdminServices.Pool poolEdit = new AdminServices.Pool();

        if (fID == "") fID = "-1";
        poolEdit.PoolID = Convert.ToInt32(fID);
        poolEdit.PoolName = fPoolName;

        if (poolEdit.PoolID > -1)
        {
            //Editted record
            proxy.PoolUpdate(CurrentPrincipal, poolEdit);
        }
        else
        {
            //insert record
            proxy.PoolInsert(CurrentPrincipal, poolEdit);
            Session["PoolName"] = null;
        }

        //unlock all
        proxy.PoolUnlockAll(CurrentPrincipal);

        gvPool.EditIndex = -1;

        SearchAndBind(int.Parse(tbPage.Text), tbSortDirection.Text, tbSortProperty.Text);

    }




    protected void gvPool_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        SearchAndBind(e.NewPageIndex, tbSortDirection.Text, tbSortProperty.Text);
       
        tbPage.Text = e.NewPageIndex.ToString();
    }
}
